package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

public class AddRemoveShipItem extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2140686958435417008L;
	private Manager manager;
	// private JFrame main;
	private JTextField supText;
	private JButton reShip, addShip;
	private JTextArea textArea;
	private JScrollPane confirmation;
	private JTextField sidText;
	private JTextField dateText;
	private JTextField upcText;
	private JTextField qText;

	public AddRemoveShipItem(Manager manager) {
		this.manager = manager;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		addPanel();
		confirmPanel();

	}

	private void addPanel() {
		JPanel adder = new JPanel();
		adder.setLayout(new GridLayout(6, 2));
		adder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

		adder.add(new JLabel("Sid of shipment:"));
		sidText = new JTextField();
		adder.add(sidText);

		adder.add(new JLabel("upc of ship item:"));
		upcText = new JTextField();
		adder.add(upcText);

		adder.add(new JLabel("Supplier's price:"));
		supText = new JTextField();
		adder.add(supText);
		
		adder.add(new JLabel("Quantity:"));
		qText = new JTextField();
		adder.add(qText);

		adder.add(new JLabel("Click to insert shipItem"));
		addShip = new JButton("add");
		addShip.addActionListener(new addShip());
		adder.add(addShip);

		adder.add(new JLabel("Click to remove ShipItem"));
		reShip = new JButton("remove");
		reShip.addActionListener(new reShip());
		adder.add(reShip);

		add(adder);
	}

	private void confirmPanel() {
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED),
				"Confirmation");
		conPanel.setBorder(title);

		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);

		confirmation = new JScrollPane(textArea);
		confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		conPanel.add(confirmation, BorderLayout.CENTER);

		add(conPanel);
	}

	public class addShip implements ActionListener {
		String upc;
		int sid, supPrice, quantity;

		public void actionPerformed(ActionEvent arg0) {
			upc = upcText.getText();
			supPrice = Integer.valueOf(supText.getText());
			sid = Integer.valueOf(sidText.getText());
			quantity = Integer.valueOf(qText.getText());
			
			try {
				manager.insertShipitem(sid, upc, supPrice, quantity);
				textArea.append("******************* Shipitem Inserted ******************" + "\n" +
								"\n" + "sid: " + sid +
								"\n" + "UPC: " + upc +
								"\n" + "Supplier's Price: " + supPrice +
								"\n" + "Quantity: " + quantity +
								"\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("Shipitem already exists or item to be shipped does not exist.  Cannot be added!");
				e.printStackTrace();
			}

		}
	}

	public class reShip implements ActionListener {
		int sid;

		public void actionPerformed(ActionEvent arg0) {
			sid = Integer.valueOf(sidText.getText());
			
			try {
				manager.deleteShipitem(sid);
				textArea.append("******************* Shipitem Deleted ******************" + "\n"
								+ "\n" + "Shipitem with sid: " + sid + " has been removed successfully"
								+ "\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("Shipitem does not exist.  Cannot be deleted!");
				e.printStackTrace();
			}

		}

	}
}
